package com.zhaolin.contorller;



import com.zhaolin.entity.Admin;
import com.zhaolin.service.adminService;
import com.zhaolin.utils.MD5Utils;
import com.zhaolin.utils.MapControl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/*@RestController*/
@Controller
@RequestMapping("/admin")
@CrossOrigin
public class AdminController {

    @Autowired
   adminService adminService;

    @GetMapping("/create")
    public String v_create(){

        return "page/admin/add";
    }

    @PostMapping("/create_s")
    @ResponseBody
    public Map<String, Object> create(@RequestBody Admin admin,ModelMap modelMap){
        String md5 = MD5Utils.getMD5(admin.getPassword());
        admin.setPassword(md5);
        int result = adminService.create(admin);
        if (result<=0){
            //失败的情况
            return MapControl.getInstance().error().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }

    @GetMapping("/list")
    public String list(){
        return "page/admin/list";
    }

    @PostMapping("/delete")
    @ResponseBody
    public Map<String,Object> delete(String ids){
        int result = adminService.deleteBatch(ids);
        if (result<=0){
            //失败的情况
            return MapControl.getInstance().error().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }

    @PostMapping("/update")
    @ResponseBody
    public Map<String, Object> update(@RequestBody Admin admin){
     int result = adminService.update(admin);
     if (result<=0){
         //失败的情况
         return MapControl.getInstance().error().getMap();
     }
     return MapControl.getInstance().success().getMap();
    }

    @PostMapping("/query")
    @ResponseBody
    public Map<String, Object> query(@RequestBody Admin admin, ModelMap modelMap){
        List<Admin> query = adminService.query(admin);
        int count = adminService.count(admin);
        return MapControl.getInstance().page(query,count).getMap();
    }

    @GetMapping("/detail")
    public String detail(Integer id,ModelMap modelMap){
        Admin detail = adminService.detail(id);
        modelMap.addAttribute("admin",detail);
        return "page/admin/update";
    }



}
